<?php

namespace App\Http\Controllers\Admin;

use App\Model\ShopHomeModel;
use App\Model\SearchLogModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Validator;

class ShopHomeController extends Controller
{
    //入口类别列表
    public function index(Request $request)
    {
        //搜索条件
        $keywords = $request->input('keywords');//关键字
        $month = $request->input('month');//时间

        $pageParam['keywords'] = $keywords;

        $list = ShopHomeModel::when($keywords, function ($query) use ($keywords) {
                return $query->where('name', 'like', "%{$keywords}%");
            })
            ->orderBy('id', 'desc')
            ->paginate(10);
        foreach($list as $k=>$v){
        	if($v['oss_pic']){
        		$v['picname'] = $v['oss_pic'];
			}else{
        		if($v['picname']){
        			$v['picname'] = 'http://'.$_SERVER['HTTP_HOST'].$v['picpath'].$v['picname'];
				}
			}
		}
        //分页
        $page = $list->appends($pageParam)->links();
        return view('admin.shophome.index')
            ->with('keywords',$keywords)
            ->with('month',$month)
            ->with('session_id',session('admin_id'))
            ->with('page',$page)
            ->with('list',$list);
    }

    //添加商品页
  	public function add(){
		//类别(搜索)
    	return view('admin.shophome.add');
  	}

	//添加商品
	public function insert(Request $request)
	{
		$post = $request->post();
		$validator = Validator::make($post,[
			'name'=>'required|unique:shop_home,name',
			'picname' => 'required',
			],[
			'name.required' => '入口类别不能为空',
			'name.unique' => '入口类别已存在',
			'picname.required' => 'Logo不能为空',
			]);
		if($validator->fails()){
			return returnJson(0, $validator->errors()->first());
		}

		DB::beginTransaction();
		try {

			$home=new ShopHomeModel();
			$home->name=$request->name;
			$home->picname=$request->picname;
			$home->oss_pic=$request->picname;
			$home->save();
			DB::commit();
		}catch(Exception $e){
			DB::rollBack();
		  	throw $e;
		  	return returnJson(0,'操作失败,请稍后再试');
		}

		return returnJson(1,'添加成功');
	}

	 //更改状态
	public function up_state(Request $request,ShopHomeModel $home){
		$state = $request->post('state');//状态
		$id = $request->post('id');

		$returnArr = $home->updateState($id,$state);
		return response()->json($returnArr);
	}

	//编辑页面
	public function edit(){

  	    $list = ShopHomeModel::where('id',request()->id)->first();
  	    return view('admin.shophome.edit',compact('list'));
	}

	//编辑商品
	public function update(Request $request){
		$post = $request->post();
    	if(ShopHomeModel::where('id','<>',$post['id'])->where('name',$post['name'])->first()){
    		return returnJson(0,'该入口类别已存在');
		}
    	//验证
		$validator = Validator::make($post,[
			'name'=>'required',
			'picname' => 'required',
			],[
			'name.required' => '入口类别不能为空',
			'picname.required' => 'Logo不能为空',
			]);
		if($validator->fails()){
			return returnJson(0, $validator->errors()->first());
		}

		if (ShopHomeModel::where('id', $post['id'])->update(["name" => request()->name,"picname"=>request()->picname,'oss_pic'=>request()->picname])) {
			return returnJson(1, '更改成功');
		} else {
			return returnJson(0, '更改失败');
		}


	}

	//删除商品
	public function del(){
		$id = request()->id;
		//入口类别信息
		$lable = ShopHomeModel::where('id',$id)->first();
		@unlink(".".$lable['picpath'].$lable['picname']);
		if(ShopHomeModel::where('id',$id)->delete()){
			return returnJson(1, '删除成功');
		}else{
			return returnJson(0, '删除失败');
		}
	}

	//更改排序
	public function update_sort(Request $request)
	{
		$region = ShopHomeModel::find($request->id);
		$region->sort = $request->sort;
		if ($region->save()) {
			return ['code' => 1, 'msg' => '修改成功'];
		}
		return ['code' => 0, 'msg' => '修改失败'];
	}

	//推荐搜索字
	public function search(){
    	$list = SearchLogModel::orderBy('id','desc')->paginate(15);
    	return view('admin.shophome.search',compact('list'));
	}


	//添加
	public function search_add(){
    	return view('admin.shophome.search_add');
	}

	public function search_insert(){
    	if(!request()->name){
    		return ['code'=>0,'msg'=>'请输入搜索关键字'];
		}
    	if(SearchLogModel::where('name',request()->name)->first()){
    		return ['code'=>0,'msg'=>'该关键字已存在'];
		}
    	SearchLogModel::create([
    		'name' => request()->name,
		]);

    	return ['code'=>1,'msg'=>'添加成功'];
	}

	//删除
	public function search_del(){
    	if(!request()->id){
    		return ['code'=>0,'msg'=>'非法入侵'];
		}
    	DB::beginTransaction();
    	try{
    		SearchLogModel::where('id',request()->id)->delete();
    		DB::commit();
    		return ['code'=>1,'msg'=>'已删除'];
		}catch(Exception $e){
    		DB::rollBack();
    		return ['code'=>0,'msg'=>'删除失败'];
		}
	}


}
